#!/usr/bin/php -q
<?PHP

 $file = '/home/dmdirc/www/junit/all-tests.html';

 if (!file_exists($file) || ($data = @file_get_contents($file)) === false) {
  echo "Unable to open $file\n";
  exit(1);
 }

 preg_match_all('#<tr[^>]*?class="Failure">(.*?)</tr>#is', $data, $m);

 foreach ($m[1] as $match) {
  preg_match_all('#<td>(.*?)</td>#is', $match, $n);

  $status = $n[1][2];

  if ($status == 'Success') { continue; }

  $stack  = array_filter(preg_split('#\s*<br\s*/?>\s*#i', str_replace(array("\r", "\n"), '', $n[1][3])));
  $stack  = preg_replace_callback('#<code>(.*?)</code>#is', create_function('$arg', 'return htmlentities($arg[1]);'), implode("\n", $stack));
  $stack  = trim(html_entity_decode($stack, ENT_COMPAT, 'UTF-8'));
  $title  = array_shift(explode("\n", $stack));
  $stack  = '[' . str_replace(', at ', ', ', str_replace("\n", ', ', $stack)) . ']';

  $data = '?trace=' . urlencode($stack) . '&version=SVN(0)&message='. urlencode('Unit test failure: ' . $title);
  file_get_contents('http://www.dmdirc.com/error.php' . $data);
 }

?>
